#include "yinmei.h"
#include <iostream>
#include <thread>
#include <chrono>

using namespace yinmei;

void printStatus(PrintStatus status) {
    switch (status) {
        case PrintStatus::IDLE:
            std::cout << "Status: IDLE" << std::endl;
            break;
        case PrintStatus::CONVERTING:
            std::cout << "Status: CONVERTING" << std::endl;
            break;
        case PrintStatus::PRINTING:
            std::cout << "Status: PRINTING" << std::endl;
            break;
        case PrintStatus::PRINT_ENDING:
            std::cout << "Status: PRINT_ENDING" << std::endl;
            break;
        case PrintStatus::COMPLETED:
            std::cout << "Status: COMPLETED" << std::endl;
            break;
        case PrintStatus::ERROR:
            std::cout << "Status: ERROR" << std::endl;
            break;
    }
}

void printConvertResult(ConvertResult result) {
    switch (result) {
        case ConvertResult::SUCCESS:
            std::cout << "Convert Result: SUCCESS" << std::endl;
            break;
        case ConvertResult::FILE_NOT_FOUND:
            std::cout << "Convert Result: FILE_NOT_FOUND" << std::endl;
            break;
        case ConvertResult::CONVERT_FAILED:
            std::cout << "Convert Result: CONVERT_FAILED" << std::endl;
            break;
        case ConvertResult::INVALID_FORMAT:
            std::cout << "Convert Result: INVALID_FORMAT" << std::endl;
            break;
    }
}

int main() {
    std::cout << "=== YinMei Printer Demo ===" << std::endl;
    
    YinmeiPrinter printer;
    
    std::cout << "\n1. Setting input file..." << std::endl;
    std::string inputFile = "../test.jpg"; 
    
    if (printer.setInputFile(inputFile)) {
        std::cout << "Input file set successfully: " << printer.getInputFile() << std::endl;
    } else {
        std::cout << "Failed to set input file: " << printer.getLastError() << std::endl;
        return -1;
    }
    
    PrinterConfig config;
    config.resolution = 300;
    config.unit = 1200;
    config.grey = 1;
    config.duplex = 120;
    config.revert = 0;
    config.angle = -90;
    printer.setPrinterConfig(config);
    
    std::cout << "\n2. Converting image..." << std::endl;
    ConvertResult convertResult = printer.convertImage();
    printConvertResult(convertResult);
    
    if (convertResult == ConvertResult::SUCCESS) {
        std::cout << "Converted file path: " << printer.getConvertedFilePath() << std::endl;
    } else {
        std::cout << "Conversion failed: " << printer.getLastError() << std::endl;
        return -1;
    }
    
    std::cout << "\n3. Starting print..." << std::endl;
    if (printer.startPrint()) {
        std::cout << "Print command sent successfully" << std::endl;
    } else {
        std::cout << "Failed to start print: " << printer.getLastError() << std::endl;
        return -1;
    }
    
    std::cout << "\n4. Monitoring print status..." << std::endl;
    std::cout << "正在监控打印状态..." << std::endl;
    
    bool printCompleted = false;
    int maxWaitTime = 180; 
    int waitedTime = 0;
    int endingStatusCount = 0; 
    
    while (!printCompleted && waitedTime < maxWaitTime) {
        PrintStatus status = printer.queryPrintStatus();
        printStatus(status);
        
        if (status == PrintStatus::PRINT_ENDING) {
            endingStatusCount++;
            std::cout << "  -> 打印结束中状态计数: " << endingStatusCount << "/10 秒" << std::endl;
        } else {
            endingStatusCount = 0; 
        }
        
        if (status == PrintStatus::ERROR) {
            std::cout << "Print error: " << printer.getLastError() << std::endl;
            break;
        }
        
        if (printer.isPrintCompleted()) {
            std::cout << "Print completed" << std::endl;
            printCompleted = true;
            break;
        }
        
        std::this_thread::sleep_for(std::chrono::seconds(1));
        waitedTime++;
        
        if (waitedTime % 10 == 0) {
            std::cout << "  -> 已等待 " << waitedTime << " 秒..." << std::endl;
        }
    }
    
    if (!printCompleted && waitedTime >= maxWaitTime) {
        std::cout << "Print status monitoring timeout" << std::endl;
    }
    
    std::cout << "\n=== Demo completed ===" << std::endl;
    return 0;
}

void advancedUsageExample() {
    std::cout << "\n=== Advanced Usage Example ===" << std::endl;
    
    YinmeiPrinter printer;
    
    printer.setWorkingDirectory("/path/to/your/work/directory");
    
    if (printer.setInputFile("/absolute/path/to/image.jpg")) {
        
        std::string customOutputPath = "/path/to/output/custom_output.bin";
        ConvertResult result = printer.convertImage(customOutputPath);
        
        if (result == ConvertResult::SUCCESS) {
            printer.startPrint(customOutputPath);
            
            while (!printer.isPrintCompleted()) {
                PrintStatus status = printer.queryPrintStatus();
                printStatus(status);
                
                if (status == PrintStatus::ERROR) {
                    std::cout << "Error: " << printer.getLastError() << std::endl;
                    break;
                }
                
                std::this_thread::sleep_for(std::chrono::seconds(1));
            }
        }
    }
}